package com.sanpu.kxe.business.main.view;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.adapter.MaterialVideoAdapter;
import com.sanpu.kxe.business.main.bean.MaterialVideoOutput;
import com.sanpu.kxe.business.main.contract.MaterialVideoContract;
import com.sanpu.kxe.business.main.presenter.MaterialVideoPresenter;
import com.sanpu.kxe.business.main.provider.MainOpenProvider;
import com.sanpu.kxe.module.widget.VerticalSwipeRefreshLayout;

import java.util.List;

import butterknife.Bind;

/**
 * Description：银信通推广视频
 * Created by 薛勇军 on 2019/9/27.
 */

public class MaterialVideoActivity extends BaseTitleActivity implements MaterialVideoContract.View {

    @Bind(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;
    @Bind(R.id.recyclerView)
    RecyclerView vRecyclerView;

    private MaterialVideoAdapter videoAdapter;

    private MaterialVideoContract.Presenter mPresenter;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_material_video;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("推广视频");
        vRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        videoAdapter = new MaterialVideoAdapter();
        videoAdapter.setEnableLoadMore(false);
        vRecyclerView.setAdapter(videoAdapter);

        mPresenter = new MaterialVideoPresenter(this);
        mPresenter.promotionVideo();
    }

    @Override
    protected void setListener() {
        super.setListener();
        videoAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (position >= adapter.getData().size()) return;
                MaterialVideoOutput videoOutput = (MaterialVideoOutput)adapter.getItem(position);
                if (videoOutput != null) {
                    MainOpenProvider.getInstance().openBrowserX5Activity(getContext(), videoOutput.getUrl(), true);
                }
            }
        });
        videoAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.shareIv) {
                    MaterialVideoOutput videoOutput = (MaterialVideoOutput)adapter.getItem(position);
                    if (videoOutput != null) {
                        mPresenter.shareLinkPath(videoOutput);
                    }
                }
            }
        });
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.promotionVideo();
            }
        });
    }

    @Override
    public void showVideoListView(List<MaterialVideoOutput> videoOutputs) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        if (videoOutputs != null && videoOutputs.size()>0) {
            videoAdapter.setNewData(videoOutputs);
            videoAdapter.notifyDataSetChanged();
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(MaterialVideoContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
